<template>
  <el-dialog v-model="visible" :title="!dataForm.openid ? $t('add') : $t('update')" :close-on-click-modal="false" :close-on-press-escape="false">
    <el-form :model="dataForm" :rules="rules" ref="dataFormRef" @keyup.enter="dataFormSubmitHandle()" label-width="120px">
      <el-form-item label="微信开放ID" prop="openid">
        <el-input v-model="dataForm.openid" placeholder="微信开放ID"></el-input>
      </el-form-item>
      <el-form-item label="UnionID" prop="unionid">
        <el-input v-model="dataForm.unionid" placeholder="微信UnionID（用于多应用用户统一）"></el-input>
      </el-form-item>
      <el-form-item label="用户名" prop="username">
        <el-input v-model="dataForm.username" placeholder="用户名"></el-input>
      </el-form-item>
      <el-form-item label="法号" prop="dharmaName">
        <el-input v-model="dataForm.dharmaName" placeholder="法号"></el-input>
      </el-form-item>
      <el-form-item label="密码" prop="password">
        <el-input v-model="dataForm.password" placeholder="密码"></el-input>
      </el-form-item>
      <el-form-item label="手机号" prop="phone">
        <el-input v-model="dataForm.phone" placeholder="手机号"></el-input>
      </el-form-item>
      <el-form-item label="是否激活" prop="isActive">
        <ren-radio-group v-model="dataForm.isActive" dict-type="is_active"></ren-radio-group>
      </el-form-item>
      <el-form-item label="是否是会员" prop="isMember">
        <ren-radio-group v-model="dataForm.isMember" dict-type="is_active"></ren-radio-group>
      </el-form-item>
      <el-form-item label="会员等级" prop="memberLevel">
        <ren-select v-model="dataForm.memberLevel" dict-type="eat_type" placeholder="会员等级"></ren-select>
      </el-form-item>
      <el-form-item label="出生日期" prop="birthDate">
        <el-date-picker type="datetime" placeholder="出生日期" v-model="dataForm.birthDate" format="YYYY-MM-DD" value-format="YYYY-MM-DD"></el-date-picker>
      </el-form-item>
      <el-form-item label="省份" prop="province">
        <el-input v-model="dataForm.province" placeholder="省份"></el-input>
      </el-form-item>
      <el-form-item label="城市" prop="city">
        <el-input v-model="dataForm.city" placeholder="城市"></el-input>
      </el-form-item>
      <el-form-item label="区县" prop="district">
        <el-input v-model="dataForm.district" placeholder="区县"></el-input>
      </el-form-item>
      <el-form-item label="详细地址" prop="detailAddress">
        <el-input v-model="dataForm.detailAddress" placeholder="详细地址"></el-input>
      </el-form-item>
      <el-form-item label="性别" prop="sex">
        <ren-radio-group v-model="dataForm.sex" dict-type="gender"></ren-radio-group>
      </el-form-item>
      <el-form-item label="头像" prop="avatarUrl">
        <el-input v-model="dataForm.avatarUrl" placeholder="头像URL"></el-input>
      </el-form-item>
      <el-form-item label="累计福报值" prop="totalMerit">
        <el-input v-model="dataForm.totalMerit" placeholder="累计福报值"></el-input>
      </el-form-item>
    </el-form>
    <template v-slot:footer>
      <el-button @click="visible = false">{{ $t("cancel") }}</el-button>
      <el-button type="primary" @click="dataFormSubmitHandle()">{{ $t("confirm") }}</el-button>
    </template>
  </el-dialog>
</template>

<script lang="ts" setup>
import { reactive, ref } from "vue";
import baseService from "@/service/baseService";
import { useI18n } from "vue-i18n";
import { ElMessage } from "element-plus";
const { t } = useI18n();
const emit = defineEmits(["refreshDataList"]);

const visible = ref(false);
const dataFormRef = ref();

const dataForm = reactive({
  id: "",
  openid: "",
  unionid: "",
  sessionKey: "",
  username: "",
  dharmaName: "",
  password: "",
  phone: "",
  isActive: "",
  isMember: "",
  memberLevel: "",
  birthDate: "",
  province: "",
  city: "",
  district: "",
  detailAddress: "",
  sex: "",
  avatarUrl: "",
  createDate: "",
  updateDate: "",
  totalMerit: "",
});

const rules = ref({
  isMember: [{ required: true, message: t("validate.required"), trigger: "blur" }],
  memberLevel: [{ required: true, message: t("validate.required"), trigger: "blur" }],
  birthDate: [{ required: true, message: t("validate.required"), trigger: "blur" }],
  sex: [{ required: true, message: t("validate.required"), trigger: "blur" }],
});

const init = (id?: number) => {
  visible.value = true;
  dataForm.id = "";

  // 重置表单数据
  if (dataFormRef.value) {
    dataFormRef.value.resetFields();
  }

  if (id) {
    getInfo(id);
  }
};

// 获取信息
const getInfo = (id: number) => {
  baseService.get("/sys/mdluser/" + id).then((res) => {
    Object.assign(dataForm, res.data);
  });
};

// 表单提交
const dataFormSubmitHandle = () => {
  dataFormRef.value.validate((valid: boolean) => {
    if (!valid) {
      return false;
    }
    (!dataForm.id ? baseService.post : baseService.put)("/sys/mdluser", dataForm).then((res) => {
      ElMessage.success({
        message: t("prompt.success"),
        duration: 500,
        onClose: () => {
          visible.value = false;
          emit("refreshDataList");
        }
      });
    });
  });
};

defineExpose({
  init
});
</script>
